/**
 * @author baz
 */
package mvc.controller {
import bot.BotStateList;
import bot.BotView;

import cat.CatStateList;
import cat.CatView;

import flash.events.Event;
import flash.geom.Point;

import lib.KeyboardManager;

import mvc.model.GameModel;

import org.robotlegs.base.ContextEvent;
import org.robotlegs.mvcs.Command;

public class AppCmd extends Command {

    [Inject]
    public var event:Event;

    [Inject]
    public var gameModel:GameModel;

    override public function execute():void {
        if (event.type == ContextEvent.STARTUP_COMPLETE) {
            KeyboardManager.stage = contextView.stage;
            contextView.addChild(new MainView());
            contextView.stage.addEventListener(Event.ENTER_FRAME, frameHandler);

            // Cat
            var catView:CatView = new CatView();
            contextView.addChild(catView);
            catView.x = contextView.stage.stageWidth / 2;
            catView.y = contextView.stage.stageHeight / 2;
            gameModel.interactiveObjects.push(catView);

            // NPCs
            var botList:Vector.<BotView> = Vector.<BotView>([]);
            for (var i:int = 0; i < 2; i++) {
                var botView:BotView = new BotView();
                contextView.addChild(botView);
                botView.setState(
                        BotStateList.CIRCLE,
                        new Point(contextView.stage.stageWidth / 2, contextView.stage.stageHeight / 2),
                        i ? 180 : 0);
                botList.push(botView);
                gameModel.interactiveObjects.push(botView);
            }

            // Переключаем состояние кота
            catView.setState(CatStateList.READY, botList);
        }
    }

    private function frameHandler(e:Event):void {
        for each (var view:IFrameObject in gameModel.interactiveObjects) {
            view.frameHandler();
        }
    }
}
}
